function LiveRaid(options)
{

    // Self reference (is there any other way to do this?)
    var self = this; 
    
    this.initialize = function()
    {
        this.refreshRaidInformation();
        this.refresh();
    } 
    
    this.refresh = function()
    {
        options.containers.roster.html("");
        options.containers.events.html("");
        
        options.containers.roster.append($.create("div", {"class":"ajax_loader"}));
        options.containers.events.append($.create("div", {"class":"ajax_loader"}));

        performCallback(    
            options.actions.getRaidEvents, 
            {"raidId": options.raid},
            displayEvents
        );
    }
    
    this.refreshRaidInformation = function()
    {
        options.containers.raid.html("");
        options.containers.raid.append($.create("div", {"class":"ajax_loader"}));
        
        performCallback(    
            options.actions.getRaidInformation, 
            { "raidId" : options.raid },
            function(data) {
                options.containers.raid.html("");
                
                var container = options.containers.raid;
                container.append($("<p><strong>Raid Leader:</strong> " + data.raidleader_name + "</p>"));
                container.append($("<p><strong>Start time:</strong> " + data.start_time + "</p>"));
                container.append($("<p><strong>Finish time:</strong> " + data.finish_time + "</p>"));
			    container.append($("<p><strong>Dungeons on the menu:</strong></p>"));
			    var dungeonList = $.create("ul").appendTo(container)
                $.each( data.dungeons, function(i, dungeon) {
                    $.create("li",{},dungeon.name).appendTo(dungeonList);
                });
                container.append($("<p><strong>The important note:</strong></p>"));
                container.append($("<p>" + data.note + "</p>"));
            }
        ); 
    }
    
    this.getOptions = function()
    {
        return options;
    }
    
    function callAndRefresh(script, params) 
    {
        performCallback(    
            script, 
            params,
            function(data) {
                self.refresh();
            }
        ); 
    }
    
    this.addJoin = function(characterId, time, note)
    {
        var params = { "raidId": options.raid, "characterId": characterId, "note" : note };
        if (time!=undefined) params.time = time;
        
        callAndRefresh(options.actions.addJoin, params);
    }
    
    this.addDungeonChange = function(dungeonId)
    {
        var params = { "raidId": options.raid, "dungeonId": dungeonId }; 
        callAndRefresh(options.actions.addDungeonChange, params);
    }
    
    this.addLeave = function(characterId, time, dkpEventId, dkpSpent, note)
    {
	    var params = { "raidId" : options.raid, 
	                   "characterId" : characterId, 
	                   "dkpEventId" : dkpEventId,
	                   "spent" : dkpSpent,
	                   "note" : note };
	                   
	    if (time!=undefined) params.time = time;
	                   
	    callAndRefresh(options.actions.addLeave, params);
    }
    
    this.addKill = function(bossId, time, dkpEventId, dkpEarned, note) 
    {
        var params = { "raidId" : options.raid, 
                       "bossId" : bossId,
                       "dkpEventId" : dkpEventId,
                       "earned": dkpEarned,
                       "note" : note };
                       
        if (time!=undefined) params.time = time;
        
        callAndRefresh(options.actions.addKill, params);
    }
    
    this.addDrop = function(killId, characterId, itemId, dkpSpent, note)
    {
        var params = { 
            "raidId" : options.raid, 
            "killId" : killId,
            "characterId" : characterId,
            "itemId": itemId,
            "dkpSpent" : dkpSpent,
            "note" : note
        };
        
        callAndRefresh(options.actions.addDrop, params);
    }
    
    this.removeDrop = function(dropId)
    {
        var params = { 
            "raidId" : options.raid, 
            "dropId" : dropId,
        };
        
        callAndRefresh(options.actions.removeDrop, params);
    }
    
    this.removeEvent = function(event)
    {
        if (event.type=="join") {
            callAndRefresh(options.actions.removeJoin, { "raidId": options.raid, "rosterChangeId": event.id });
        } else if (event.type=="leave") {
            callAndRefresh(options.actions.removeLeave, { "raidId": options.raid, "rosterChangeId": event.id });
        } else if (event.type=="kill") {
            callAndRefresh(options.actions.removeKill, { "raidId": options.raid, "raidKillId": event.id });
        } else {
            alert("unhandled remove event: " + event.type);
        }
    }
    
    function displayEvents(events)
    {
        // Clear the containers
        options.containers.roster.html("");
        options.containers.events.html("");
        
        // Add a roster entry selector to the top of the roster list
        var rosterAdd = new RosterAddSelector({ "liveRaid" : self, "action": options.actions.getCharactersInRaid});
        options.containers.roster.append(rosterAdd.getElement());
        
        // Loop over all received events
        $.each( events, function(i, event) {
        
            // Events with time 0 should be roster start
            if( event.time==0 ) {
                if(event.type=="join") {
                    var rosterEntry = new RosterEntry({ "liveRaid" : self, "event" : event});
                    options.containers.roster.append(rosterEntry.getElement());
                } else {
                    alert("Event at time 0 was not a join!");
                }  
            } else {
                var eventInsert = new EventInsert({ "liveRaid" : self, "event" : event});
                options.containers.events.append(eventInsert.getElement());
                
                var eventEntry = new Event({ "liveRaid" : self, "event" : event});
                options.containers.events.append(eventEntry.getElement());
            }
        });
        
        var eventInsertPrimary = new EventInsert({ "liveRaid" : self, "primary" : true});
        options.containers.events.append(eventInsertPrimary.getElement());
    }
    
    function performCallback(script, params, callbackFunction)
    {
        $.post( 
            script,
            params,
            function(response) { 
                if (response.status != 200) {
                    alert(response.message);
                } else {
                    callbackFunction(response.data);
                }
            },
            'json'
        );
    }
}

function Event(options)
{
    var element = $.create("div");
    var frame = new Frame({"width":"100%", "center": false, "padding":"5px", "style":"metal"}).appendTo(element);
    var headline = $.create("h1", {}).appendTo(frame.getContainer());
    var subContainer = $.create("div").appendTo(frame.getContainer()).hide();
    
    headline.css("cursor", "pointer");
    headline.click(function() {
        subContainer.slideToggle("fast");
    });
    
    var btnRemove = $.create("div", {"class":"button_small"}, "Remove").click( function() {
        options.liveRaid.removeEvent(options.event);
    });
    
    //frame.append($.create("p", {}, options.event.type));
    //frame.append($.create("p", {}, html_print_r(options.event)));
    
    if (options.event.type == "join") {
        showJoinEvent();
    } else if (options.event.type == "leave") {
        showLeaveEvent();
    } else if (options.event.type == "kill") {
        showKillEvent();
    } else if (options.event.type == "dungeon") {
        showDungeonEvent();
    } else {
        headline.append("Event type: " + options.event.type);
        //subContainer.append(html_print_r(options.event));
    }
    
    //subContainer.append(html_print_r(options.event));
    
    function showJoinEvent()
    {
        headline.append($.create("img", {"class":"live_icon", "src" : pldkpPaths.icons + "join.jpg"}));
        headline.append($.create('img', {'src': pldkpPaths.base + options.event.class_icon_small}));
        headline.append(" ");
        headline.append($.create("strong", {}, options.event.character_name));
        headline.append(" joins the raid");
        
        if (options.event.note) {
            subContainer.append($("<p><em>" + options.event.note + "</em></p>"));
        }
        
        subContainer.append(btnRemove);
    }
    
    function showLeaveEvent()
    {
        headline.append($.create("img", {"class":"live_icon", "src" : pldkpPaths.icons + "leave.jpg"}));
        headline.append($.create("strong", {}, options.event.character_name));
        headline.append(" leaves the raid");
        
        if (options.event.note) {
            subContainer.append($("<p><em>" + options.event.note + "</em></p>"));
        }
        
        subContainer.append(btnRemove);
    }
    
    function showKillEvent()
    {
        headline.append($.create("img", {"class":"live_icon", "src" : pldkpPaths.icons + "kill.jpg"}));
        headline.append("The raid kills ");
        headline.append($.create("strong", {}, options.event.boss.name));
        
        subContainer.append(btnRemove);
        
        if (options.event.note) {
            subContainer.append($("<p><em>" + options.event.note + "</em></p>"));
        }
        
        subContainer.append($("<p>This was a <strong>" + options.event.dkp_adjustment.dkp_event_name + "</strong> " + 
                              "that awarded <strong>" + options.event.dkp_adjustment.dkp_earned + " dkp</strong> " +
                              "to the following characters:</p>"));
        
        var charList = $.create("ul").appendTo(subContainer);
        
        $.each( options.event.dkp_adjustment.characters, function(i, character) {
            $.create("li", {}, character.character_name).appendTo(charList);
        });
        
        var killDrop = new KillDrops({ "liveRaid" : options.liveRaid, "event": options.event });
        subContainer.append(killDrop.getElement());
        
        /*
        var btnAddDrop = $.create("div", {"class":"button_small"}, "Add Drop").click( function() {
            alert("add drop");
            var addDropContainer = new AddDrop({ "liveRaid" : options.liveRaid, "event": options.event } );
            addDropContainer.getElement().appendTo(subContainer);
        });
        */
        
        
    }
    
    function showDungeonEvent()
    {
        frame.setBackground(pldkpPaths.site + "fade_bg_red.png");
        headline.append($.create("img", {"class":"live_icon", "src" : pldkpPaths.icons + "dungeon.jpg"}));
        headline.append("The raid enters ");
        headline.append($.create("strong", {}, options.event.dungeon_name));
        
        if (!options.event.is_last_event) {
            btnRemove.addClass("small_disabled");
            btnRemove.unbind("click");
        }
        subContainer.append(btnRemove);
    }
    
    this.getElement = function()
    {
        return element;
    }
}

function KillDrops(options)
{
    var element = $.create("div");
    
    // Create a frame for the add drop
    var frame = new Frame({"width":"70%", "center": true, "padding":"5px", "style":"input"}).appendTo(element);
    var container = frame.getContainer();
    
    showDrops();
    
    // This function shows the list of items dropped for the active kill
    function showDrops() 
    {
        container.html("");
 
        var dropsContainer = $.create("div");
        
        $.each(options.event.drops, function(i, drop) {
            
            var dropDiv = $.create("div", {"class": "live_raid_drop"});
            var dropText = $.create("span", {"class": "live_raid_drop_text"}).appendTo(dropDiv);
            
            var itemLink = $.create(
                "a", 
                {"href" : "http://www.wowhead.com/?item=" + drop.item_id, "class": "q" + drop.item_quality, "target": "_blank"},
                drop.item_name
            ).appendTo(dropText);
            
            if (drop.dkp_adjustment) {
                $.create("span", {}, " awarded to " + drop.character_name + " for " + drop.dkp_adjustment.dkp_spent + " dkp.").appendTo(dropText);
            } else {
                $.create("span", {}, " awarded to " + drop.character_name + " for free.").appendTo(dropText);
            }
            
            var dropRemove = $.create("span", {"class": "live_raid_drop_remove"}, "Remove").appendTo(dropDiv);
            dropRemove.click( function() {
                options.liveRaid.removeDrop( drop.id );
            });
            
            dropsContainer.append(dropDiv);
        });
        
        var btnAdd = $.create("div", {"class":"button_small"}, "Add Drop").click( function() {
           showAddDropForm();
        });
        
        container.append($("<p><b>Drops:</b></p>"));
        container.append(dropsContainer);
        container.append($("<br/>"));
        container.append(btnAdd);
    }
    
    function showAddDropForm()
    {
        container.html("");
          
        var characterPicker = new CharacterPicker({"liveRaid": options.liveRaid, "characters": options.event.dkp_adjustment.characters});
        var itemWinner = $.create("p", {}, "Click find winner");
        itemWinner.attr("character_id",0);
        
        var note = $.create("textarea");
        var noteFrame = new Frame({"width":"200px", "center": true,"style":"input", "padding": "0px"}).append(note);
        
        var textSpent = $.create("input", {"type":"text", "size":"5"}); 
        
        // Create a button that uses the character picker to find the winner of the item
        // Parameters for the ajax call are put into array notation
        var btnFindWinner = $.create("div", {"class":"button_small"}, "Find winner").click( function() {
            
            var params = {};
            var selected = characterPicker.getSelected();
            var count = 0;
            
            // Create parameters as array while counting (yes this is apparently the only way to do this)
            $.each(selected, function(i, id) {
                params['characterIds['+i+']'] = id;
                count++;
            });
            
            // Inform the user that no characters was selected
            if (count == 0) {
                alert("No characters selected!");
                return;
            }
            
            $.post( 
                options.liveRaid.getOptions().actions.evaluateBidders,
                params,
                function(response) { 
                    if (response.status != 200) {
                        alert(response.message);
                        itemWinner.attr("character_id", 0);
                        return;
                    }
                    
                    if (response.data.tie) {
                        itemWinner.html("There was a tie for the item. Everyone that wants the item and has buy power " + response.data.winner.buy_power + " should roll.");
                        itemWinner.attr("character_id", 0);
                        return;
                    }
                    
                    var winner = response.data.winner;
                    
                    itemWinner.html("" + winner.character_name + " wins with a buy power of " + winner.buy_power);
                    itemWinner.attr("character_id", winner.character_id);
                },
                'json'
            );
            
        });
        
        var itemList = getItemsForBoss(options.event.boss.id);

        var selectItem = $.create("select");
        $.create("option", {"value":""}, "...").appendTo(selectItem).click( function() {
            $(textSpent).val("");
        });
        
        $.each(itemList, function(i, item) {
            $.create("option", {"value":item.id}, item.name).appendTo(selectItem).click( function() {
                $(textSpent).val(item.dkpPrice);
            });
        });
        
        var btnAddDrop = $.create("div", {"class":"button_small"}, "Award item").click( function() {
        
            var winnerId = itemWinner.attr("character_id");
            if (winnerId == 0) {
                alert("No winner found yet.");
                return;
            }
            
            var itemId = $(selectItem).val();
            
            var dkpSpent = $(textSpent).val();
            
            var noteText = note.val();

            options.liveRaid.addDrop(
                options.event.id, 
                winnerId,
                itemId, 
                dkpSpent, 
                noteText
            );
        });
        
        var btnAbort = $.create("div", {"class":"button_small"}, "Abort").click( function() {
           showDrops();
        }); 
        
        container.append($("<p>"));
        container.append($("<b>What item dropped?</b>"));
        container.append($("<br/>"));
        container.append($("<em>If the item is not in the list, go to the items interface to add it.</em>"));
        container.append($("</p>"));
        container.append($.create("div", {"class":"input_large"}, selectItem));
        container.append($("<p><b>What should the item cost (use . for decimal)?</b></p>"));
        container.append($.create("div", {"class":"input_large"}, textSpent));
        container.append($("<p><b>Who wants the item?</b></p>"));
        container.append(characterPicker.getElement());
        container.append($("</br>"));
        container.append(btnFindWinner);
        container.append($("<p><b>And the winner is...</b></p>"));
        container.append(itemWinner);
        container.append($("<p>"));
        container.append($("<p><b>Anything we should know?</b></p>"));
        container.append($("<p>"));
        container.append(noteFrame.getElement());
        container.append($("</p>"));
        container.append(btnAddDrop);
        container.append(btnAbort);
        container.append($("<p/>"));
    }
    
    function getItemsForBoss(bossId)
    {
        var itemList = {};

        $.ajax({
            url: options.liveRaid.getOptions().actions.getBossItems,
            data: { "bossId" : bossId },
            async: false,
            type: 'POST',
            dataType: 'json',
            success: function(response) { 
                if (response.status != 200) {
                    alert("Something went horrible wrong, sorry!");
                    showDrops();
                    return;
                } 
                itemList = response.data;
            },
            error: function(response) { 
                alert("Something went horrible wrong, sorry!");
                showDrops();
            }
        });
        
        return itemList;
    }
    
    this.getElement = function()
    {
        return element;
    }
}

function CharacterPicker(options) 
{
    var element = $.create("div", {"class":"liveraid_character_picker"});
    
    var params = {};
    $.each(options.characters, function(i, character) {
        params['characterIds['+i+']'] = character.character_id;
    });
            
    $.post( 
        options.liveRaid.getOptions().actions.getCharactersBuyPower,
        params,
        function(response) { 
            if (response.status != 200) {
                alert(response.message);
                return;
            }
            
            $.each(response.data, function(id, character) {
                var characterDiv = $.create("div", {"class":"liveraid_picker_character"}).appendTo(element);
                characterDiv.append($.create("span", {"class":"liveraid_picker_character_name"}, character.character_name));
		        characterDiv.append($.create("span", {"class":"liveraid_picker_buy_power"}, character.buy_power));
		        
		        characterDiv.attr("character_id", character.character_id);
		        characterDiv.attr("character_name", character.character_name);
		        characterDiv.click( function() {
		            if (characterDiv.hasClass("liveraid_picker_character_selected")) {
		                characterDiv.removeClass("liveraid_picker_character_selected");
		            } else {
		                characterDiv.addClass("liveraid_picker_character_selected");
		            }
		        });
		    });

        },
        'json'
    );
    
    this.getSelected = function()
    {
        var selected = {};
        
        var index = 0;
        element.children().each( function() {
            if ($(this).hasClass("liveraid_picker_character_selected")) {
                var id = $(this).attr("character_id");
                selected[index++] = id;
            }
        });
        
        return selected;
    }
    
    this.getElement = function()
    {
        return element;
    }
}

function EventInsert(options)
{
    var element = $.create("div", {"class":'live_event_insert'});
    var frame = new Frame({"width":"300px", "center": true,"style":"metal"}).appendTo(element);
    frame.getElement().hide();
    
    showSelectForm();
    
    function showSelectForm()
    {
        frame.clearAndResize("300px");
        
        // Display a nice hover effect
	    $(element).hover(
	        function () { 
	            if (frame.getElement().is(':hidden')) {
	                $(element).addClass("live_event_insert_hover"); 
	            }
	        }, 
	        function () { 
	            $(element).removeClass("live_event_insert_hover"); 
	        }   
	    );
	    
	    // Toggle the buttons on and off by clicking
	    $(element).click( function() {
	        if (frame.getElement().is(':hidden')) {
	            $(frame.getElement()).slideToggle("fast");
	            $(element).removeClass("live_event_insert_hover"); 
	        }
	    });
	    
	    // Create some buttons
	    var btnDungeon = $.create("div", {"class":"button_large"}, "Enter Dungeon").click( function() {
	        showDungeonForm();
	    });
	    
	    var btnJoin = $.create("div", {"class":"button_small"}, "Add Join").click( function() {
            showJoinForm();
        });
        
	    var btnLeave = $.create("div", {"class":"button_small"}, "Add Leave").click( function() {
            showLeaveForm();
        });
        
	    var btnKill = $.create("div", {"class":"button_small"}, "Add Kill").click( function() {
            showKillForm();
        });
        
	    var btnAbort = $.create("div", {"class":"button_small"}, "Abort").click( function() {
	        $(frame.getElement()).slideUp("fast");
	    });
	    
	    if (options.primary) {
	        frame.append(btnDungeon);
	        frame.append($("<br/>"));
	    }
	    
	    frame.append(btnJoin);
	    frame.append($("<br/>"));
	    frame.append(btnLeave);
	    frame.append($("<br/>"));
	    frame.append(btnKill);
	    frame.append($("<br/>"));
	    frame.append(btnAbort);
    }
    
    function showJoinForm()
    {
        frame.clearAndResize("400px");
        
        var characters = new AutoSelector({"script": options.liveRaid.getOptions().actions.getCharactersInRaid,
                                          "params": {"inverted":"true"}});                          
        if (options.event != undefined) {
            characters.setParam("time", options.event.time - 1);
        }
        
        var note = $.create("textarea");
        var noteFrame = new Frame({"width":"200px", "center": true,"style":"input", "padding": "0px"}).append(note);
        
        
        var btnJoin  = $.create("div", {"class":"button_small"}, "Join").click( function() {
            options.liveRaid.addJoin(characters.getId(), (options.event)?options.event.time:undefined, note.val());
        });
        
        var btnAbort = $.create("div", {"class":"button_small"}, "Abort").click( function() {
            showSelectForm();
        });
        
        var container = frame.getContainer();
        container.append($("<p><b>Who joined the raid?</b></p>"));
        container.append($("<p>"));
        container.append( $.create("div", {"class":"input_large"}, characters.getElement()));
        container.append($("</p>"));
        container.append($("<p><b>Anything we should know?</b></p>"));
        container.append($("<p>"));
        container.append(noteFrame.getElement());
        container.append($("</p>"));
        container.append(btnJoin);
        container.append(btnAbort);
    }
    
    function showLeaveForm()
    {
        var leaveEvents = options.liveRaid.getOptions().events.leave;
        
        frame.clearAndResize("400px");
        
        var characters = new AutoSelector({"script": options.liveRaid.getOptions().actions.getCharactersInRaid}); 
        if (options.event != undefined) {
            characters.setParam("time",options.event.time);
        }
        
        var select = $.create("select");    
        var textSpent = $.create("input", {"type":"text", "size":"5"});  

        $.each(leaveEvents, function(id, event) {
            var option = $.create("option", {"value":id, "spent":event.spent}, event.text).appendTo(select);
            if (event.standard) {
                $(option).attr("selected", true);
                $(textSpent).val(event.spent);
            }
        });  
        
        $(select).change( function() {
            var event = leaveEvents[$(select).val()];
            $(textSpent).val(event.spent);
        });
        
        var note = $.create("textarea");
        var noteFrame = new Frame({"width":"200px", "center": true,"style":"input", "padding": "0px"}).append(note);
        
        var btnLeave  = $.create("div", {"class":"button_small"}, "Ok").click( function() {
            options.liveRaid.addLeave(characters.getId(), (options.event)?options.event.time:undefined, $(select).val(), $(textSpent).val(), note.val());
        });
        
        var btnAbort = $.create("div", {"class":"button_small"}, "Abort").click( function() {
            showSelectForm();
        });
        
        var container = frame.getContainer();
        container.append($("<p><b>Who left the raid?</b></p>"));
        container.append($("<p>"));
        container.append($.create("div", {"class":"input_large"}, characters.getElement()));
        container.append($("</p>"));
        container.append($("<p><b>Should there be DKP a penality for leaving?</b></p>"));
        container.append($("<p>"));
        container.append($.create("div", {"class":"input_large"}, select));
        container.append($("<br/>"));
        container.append($.create("div", {"class":"input_large"}, textSpent));
        container.append($("</p>"));
        container.append($("<p><b>Anything we should know?</b></p>"));
        container.append($("<p>"));
        container.append(noteFrame.getElement());
        container.append($("</p>"));
        container.append(btnLeave);
        container.append(btnAbort);          
    }
    
    function showKillForm()
    {
        var killEvents = options.liveRaid.getOptions().events.kill;
        
        frame.clearAndResize("400px");
        
        var selectBoss = $.create("select");
        var selectEvent = $.create("select");
        var textEarned = $.create("input", {"type":"text", "size":"5"});
        
        var params = { "raidId": options.liveRaid.getOptions().raid };
        if (options.event != undefined) {
            params.time = options.event.time;
        }
    
        $.post( options.liveRaid.getOptions().actions.getAliveBosses,
                params,
                function(response) { 
                    if (response.status != 200) {
                        alert("Raid is not in a dungeon yet, please enter one!");
                        showSelectForm();
                        return;
                    }
                    if (response.data.length == 0) {
                        alert("Nothing left to kill in the dungeon, sorry!");
                        showSelectForm();
                        return;
                    }

                    $.each(response.data, function(id, boss) {
                        $.create("option", {"value":boss.id}, boss.name).appendTo(selectBoss);
                    });
                },
                'json'
        );
        
        $.each(killEvents, function(id, event) {
	        var option = $.create("option", {"value":id, "earned":event.earned}, event.text).appendTo(selectEvent);
	        if (event.standard) {
	            $(option).attr("selected", true);
	            $(textEarned).val(event.earned);
	        }
	    });
	    
	    $(selectEvent).change( function() {
	        var event = killEvents[$(selectEvent).val()];
	        $(textEarned).val(event.earned);
	    });
	    
	    var note = $.create("textarea");
        var noteFrame = new Frame({"width":"200px", "center": true,"style":"input", "padding": "0px"}).append(note);
	    
	    var btnOk  = $.create("div", {"class":"button_small"}, "Ok").click( function() {
            //options.liveRaid.addDungeonChange($(select).val());
            options.liveRaid.addKill($(selectBoss).val(), (options.event)?options.event.time:undefined, $(selectEvent).val(), $(textEarned).val(), note.val());
        });
        
        var btnAbort = $.create("div", {"class":"button_small"}, "Abort").click( function() {
            showSelectForm();
            //options.liveRaid.refresh();
        });
        
        var container = frame.getContainer();
        container.append($("<p><b>What boss was killed?</b></p>"));
        container.append($("<p>"));
        container.append( $.create("div", {"class":"input_large"}, selectBoss));
        container.append($("</p>"));
        container.append($("<p><b>What kind of kill was it?</b></p>"));
        container.append($("<p>"));
        container.append( $.create("div", {"class":"input_large"}, selectEvent));
        container.append($("</p>"));
        container.append($("<p><b>How much DKP did the kill award?</b></p>"));
        container.append($("<p>"));
        container.append( $.create("div", {"class":"input_large"}, textEarned));
        container.append($("</p>"));
        container.append($("</p>"));
        container.append($("<p><b>Anything we should know?</b></p>"));
        container.append($("<p>"));
        container.append(noteFrame.getElement());
        container.append($("</p>"));
        container.append(btnOk);
        container.append(btnAbort);
    }
    
    function showDungeonForm()
    {
        var dungeons = options.liveRaid.getOptions().dungeons;
    
        frame.clearAndResize("400px");
        
        var select = $.create("select");
        
        $.each(dungeons, function(id, dungeon) {
            var option = $.create("option", {"value":id}, dungeon.name).appendTo(select);
        });
        
        var btnChange  = $.create("div", {"class":"button_small"}, "Change").click( function() {
            options.liveRaid.addDungeonChange($(select).val());
        });
        
        var btnAbort = $.create("div", {"class":"button_small"}, "Abort").click( function() {
            showSelectForm();
            //options.liveRaid.refresh();
        });
        
        var container = frame.getContainer();
        container.append($("<p><b>What dungeon did the raid enter?</b></p>"));
        container.append($("<p>"));
        container.append( $.create("div", {"class":"input_large"}, select));
        container.append($("</p>"));
        container.append(btnChange);
        container.append(btnAbort);
    }

    this.getElement = function()
    {
        return element;
    }
}

function RosterEntry(options)
{
    var element = $.create("div", {"class": 'live_roster_entry'});
    
    // Create a link for removing this roster entry
    var removeLink = $.create('a', {'class': "live_remove_link"}, "Remove");
    $(removeLink).click( function() { 
        options.liveRaid.removeEvent(options.event); 
    });
    
    // Add the char name and remove link to the element
    element.append($.create('img', {'src': pldkpPaths.base + options.event.class_icon_small}));
    element.append(' ' + options.event.character_name);
    element.append(removeLink);
    
    this.getElement = function()
    {
        return element;
    }
}

function RosterAddSelector(options) 
{
    var element = $.create("div", {"class":"live_roster_add"});
    //var frame = new MetalFrame({"width":"100%", "center": true}).appendTo(element);
    
    // Add a character selector and button for the inital join group
    var selector = new AutoSelector({
                            "script"   : options.action,
                            "params"   : {"inverted":"true", "time":0},
                            "callback" : function(id) {
                                            options.liveRaid.addJoin(id, 0);
                                         }
                       });
    var selecterContainer = $.create("div", {"class": 'input_large'}, selector.getElement());
    
    element.append($("<strong>Type a character name to add them:</strong><br/>"));
    element.append(selecterContainer);
    
    this.getElement = function()
    {
        return element;
    }
}
